package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.TbItemCatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbItemCat")
public class TbItemCatController {

    //注入service
    @DubboReference
    private TbItemCatService tbItemCatService;

    //查询分类数据
    @ResponseBody
    @RequestMapping("TbItemCatshow")
    public Result tbItemCatshow(Long parentId) {
        try {
            List<TbItemCat> list = tbItemCatService.tbItemCatshow(parentId);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(list);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加分类数据
    @ResponseBody
    @RequestMapping("addTbItemCatshow")
    public Result addTbItemCatshow(TbItemCat tbItemCat) {
        try {
            tbItemCatService.addTbItemCatshow(tbItemCat);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //回显单个分类数据
    @ResponseBody
    @RequestMapping("echoTbItemCat")
    public Result echoTbItemCat(Long id) {
        try {
            TbItemCat tbItemCat = tbItemCatService.echoTbItemCat(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(tbItemCat);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改分类数据
    @ResponseBody
    @RequestMapping("updateTbItemCatshow")
    public Result updateTbItemCatshow(TbItemCat tbItemCat) {
        try {
            tbItemCatService.updateTbItemCatshow(tbItemCat);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个分类数据
    @ResponseBody
    @RequestMapping("deleteTbItemCat")
    public Result deleteTbItemCat(Long id) {
        try {
            List<TbItemCat> list = tbItemCatService.tbItemCatshow(id);
            if (list.size() != 0) {
                return Result.response(ResultEnums.DELETE_ERRORBYZI);
            } else {
                tbItemCatService.deleteTbItemCat(id);
                return Result.response(ResultEnums.DELETE_SUCCESS);
            }
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除单个分类数据
    @ResponseBody
    @RequestMapping("deleteallTbItemCat")
    public Result deleteallTbItemCat(@RequestParam(value = "ids[]") List<Long> ids) {
        Integer ii = 0;
        for (int i = 0; i < ids.size(); i++) {
            try {
                List<TbItemCat> list = tbItemCatService.tbItemCatshow(ids.get(i));
                if (list.size() != 0) {
                    ii++;
                } else {
                    tbItemCatService.deleteTbItemCat(ids.get(i));
                }
            } catch (Exception e) {
                return Result.response(ResultEnums.DELETE_ERROR);
            }
        }
        if (ii == 0) {
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } else
            return new Result(-4, "删除个别已选,你有 " + ii + "个选项下有数据,请您先删除选项下的数据在尝试删除");
    }
}
